﻿using Basic.OADAL;
using Basic.OAModel.DB;
using Basic.OARemoteModel;
using Basic.OARemoteModel.FillInItem.Model;
using Basic.OARemoteModel.UserFill.Model;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.OACollect.lmpl
{
    internal class EmpFillInItemCollect : IEmpFillInItemCollect
    {
        private readonly IEmpFillInItemDAL _BasicDAL;

        public EmpFillInItemCollect ( IEmpFillInItemDAL fillInItem )
        {
            this._BasicDAL = fillInItem;
        }
        public UserFillData GetItem ( long id )
        {
            return this._BasicDAL.GetItem(id);
        }
        public UserFillItem[] QueryFillItem ( UserFillItemQuery query, IBasicPage paging, out int count )
        {
            return this._BasicDAL.QueryFillItem(query, paging, out count);
        }
        public void Sync ( long fillInId, FillInEmp[] adds )
        {
            this._BasicDAL.Sync(fillInId, adds);
        }
        public long[] Query ( FillInItemQuery query, IBasicPage paging, out int count )
        {
            return this._BasicDAL.Query(query, paging, out count);
        }
        public Result[] Query<Result> ( FillInItemQuery query, IBasicPage paging, out int count ) where Result : class
        {
            return this._BasicDAL.Query<Result>(query, paging, out count);
        }
        public Dictionary<long, int> GetFillEmpNum ( long[] fillInId )
        {
            if ( fillInId.IsNull() )
            {
                return null;
            }
            var list = this._BasicDAL.GroupBy(a => fillInId.Contains(a.FillInId) && a.Status == EmpFillInStatus.已填, a => a.FillInId, a => new
            {
                a.FillInId,
                num = SqlFunc.AggregateCount(a.FillInId)
            });
            return list.ToDictionary(a => a.FillInId, a => a.num);
        }
        public DBEmpFillInItem Get ( long fillId, long empId )
        {
            return this._BasicDAL.Get<DBEmpFillInItem>(a => a.FillInId == fillId && a.EmpId == empId);
        }
        public DBEmpFillInItem Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }
        public bool Revoke ( DBEmpFillInItem item )
        {
            if ( item.Status == EmpFillInStatus.未填 )
            {
                throw new ErrorException("oa.fill.item.no.fill");
            }
            if ( item.Status == EmpFillInStatus.已撤回 )
            {
                return false;
            }
            this._BasicDAL.Revoke(item);
            return true;
        }
        public bool Submit ( DBEmpFillInItem item )
        {
            if ( item.Status == EmpFillInStatus.已填 )
            {
                return false;
            }
            this._BasicDAL.Submit(item);
            return true;
        }
        public void Delete ( DBEmpFillInItem item )
        {
            if ( item.Status == EmpFillInStatus.已填 )
            {
                throw new ErrorException("oa.fill.item.already.fill");
            }
            this._BasicDAL.Delete(item.Id);
        }
        public bool CheckIsNull ( long fillId )
        {
            return this._BasicDAL.IsExists(a => a.FillInId == fillId) == false;
        }

        public int Count ( long fillId )
        {
            return this._BasicDAL.Count(a => a.FillInId == fillId);
        }
    }
}
